package com.citc;

import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author xiejp on 2022/5/18
 */
class NetMall {

    @Getter
    private String mallName;

    public NetMall(String mallName) {
        this.mallName = mallName;
    }

    public double calcPrice(String productName) {
        //检索1秒钟
        try {
            TimeUnit.SECONDS.sleep(1L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ThreadLocalRandom.current().nextDouble() * 2 + productName.charAt(0);
    }
}


public class CompletableFutureNetMallDemo {

    static List<NetMall> list = Arrays.asList(
            new NetMall("jd"),
            new NetMall("pdd"),
            new NetMall("tmall")
    );

    public static List<String> getPriceByStep(List<NetMall> list, String productName) {
        return list
                .stream()
                .map(f -> String.format(productName + " in %s price is %.2f", f.getMallName(), f.calcPrice(productName)))
                .collect(Collectors.toList());
    }

    public static List<String> getPriceByAsync(List<NetMall> list, String productName) {
        return list
                .stream()
                .map
                        (netMall -> CompletableFuture.supplyAsync(() -> String.format(productName + " in %s price is %.2f", netMall.getMallName(), netMall.calcPrice(productName))))
                .collect(Collectors.toList())
                .stream()
                .map(CompletableFuture::join)
                .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        //extracted();
        long startTime = System.currentTimeMillis();
        List<String> list = getPriceByStep(CompletableFutureNetMallDemo.list, "mysql");
        for (String s : list) {
            System.out.println(s);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗时"+(endTime-startTime));

        long startTime1 = System.currentTimeMillis();
        List<String> list1 = getPriceByAsync(CompletableFutureNetMallDemo.list, "mysql");
        for (String s : list1) {
            System.out.println(s);
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("耗时"+(endTime1-startTime1));

    }

    private static void extracted() {
        CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2L);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("------result:" + "\t" + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            return null;
        });
        System.out.println("-------" + "\t" + "----main");
        try {
            TimeUnit.SECONDS.sleep(2L);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
